import { createRouter, createWebHistory } from 'vue-router'

import {ElMessage } from 'element-plus'

import Homesys from '../views/Sysadmin/Home'

import store from '../store'

const routes = [
	// 登录
	{
		path: '/',
		name: 'Login',
		meta: {
			title: '登录'
		},
		component: () => import('../views/Login/Login')
	},
	// 后台系统
	{
		path: '/dist/homesys',
		name: 'Homesys',
		component: Homesys,
		children: [
			// 学生页
			{
				path: '/dist/personal',
				name: 'Person',
				meta: {
					title: '个人信息',
					requireAuth: true
				},
				component: () => import ('../views/Student/Personalinfo.vue')
			},
			{
				path: '/dist/choosebed',
				redirect: '/choosebed/step1' //重定向
			},
			{
				path: '/dist/choosebed',
				name: 'Bed',
				meta: {
					title: '选择床位',
				},
				component: () => import ('../views/Student/Bed.vue'),
				children: [
					{
						path: '/dist/choosebed/step1',
						name: 'step1',
						meta: {
							title: '第一步',
							requireAuth: true
						},
						component: () => import('../views/Student/Step1.vue'),
					},
					{
						path: '/dist/choosebed/step2',
						name: 'step2',
						meta: {
							title: '第二步',
						},
						component: () => import('../views/Student/Step2.vue'),
					},
					{
						path: '/dist/choosebed/step3',
						name: 'step3',
						meta: {
							title: '第三步'
						},
						component: () => import('../views/Student/Step3.vue'),
					},
					{
						path: '/dist/choosebed/step4',
						name: 'step4',
						meta: {
							title: '第四步'
						},
						component: () => import('../views/Student/Step4.vue'),
					}
				]
			},
			{
				path: '/dist/selected',
				name: 'Selected',
				meta: {
					title: '已选择',
					requireAuth: true
				},
				component: () => import ('../views/Student/Selected.vue')
			},
			{
				path: '/dist/payed',
				name: 'Payed',
				meta: {
					title: '已缴费',
					requireAuth: true
				},
				component: () => import ('../views/Student/Payed.vue')
			},
			{
				path: '/dist/pay',
				name: 'Pay',
				meta: {
					title: '在线缴费',
					requireAuth: true
				},
				component: () => import ('../views/Student/Pay.vue')
			},
			{
				path: '/dist/hardship',
				name: 'Hardship',
				meta: {
					title: '困难申请',
					requireAuth: true
				},
				component: () => import ('../views/Student/Hardship.vue')
			},
			// 学校管理人员
			{
				path: '/dist/manage',
				name: 'Manage',
				meta: {
					title: '公告管理',
					requireAuth: true
				},
				component: () => import ('../views/SchoolManager/InfoManage.vue'),
			},
			{
				path: '/dist/manage/news',
				name: 'NewsManage',
				meta: {
					title: '新闻管理',
					requireAuth: true
				},
				component: () => import ('../views/SchoolManager/NewsManage.vue')
			},
			{
				path: '/dist/manage/announce',
				name: 'NotifyManage',
				meta: {
					title: '通知管理',
					requireAuth: true
				},
				component: () => import ('../views/SchoolManager/NotifyManage.vue')
			},
			{
				path: '/dist/manage/release',
				name: 'Release',
				meta: {
					title: '发布',
					requireAuth: true
				},
				component: () => import ('../views/SchoolManager/Release.vue')
			},
			{
				path: '/dist/registration',
				name: 'Registration',
				meta: {
					title: '报到信息',
					requireAuth: true
				},
				component: () => import ('../views/SchoolManager/RegistrationInfo.vue')
			},
			{
				path: '/dist/payment',
				name: 'Payment',
				meta: {
					title: '缴费信息',
					requireAuth: true
				},
				component: () => import ('../views/SchoolManager/PaymentInfo.vue')
			},
			{
				path: '/dist/view',
				name: 'View',
				meta: {
					title: '学生信息',
					requireAuth: true
				},
				component: () => import ('../views/SchoolManager/StudentOverview.vue')
			},
			{
				path: '/dist/audit',
				name: 'Audit',
				meta: {
					title: '困难审核',
					requireAuth: true
				},
				component: () => import ('../views/SchoolManager/Audit.vue')
			},
			{
				path: '/dist/academy',
				name: 'Academy',
				meta: {
					title: '学院管理',
					requireAuth: true
				},
				component: () => import ('../views/SchoolManager/Academy.vue')
			},
			{
				path: '/dist/class',
				name: 'Class',
				meta: {
					title: '班级管理',
					requireAuth: true
				},
				component: () => import ('../views/SchoolManager/Class.vue')
			},
			// 系统管理员
			{
				path: '/dist/permission',
				name: 'Roles',
				meta: {
					title: '权限管理',
					requireAuth: true
				},
				component: () => import ('../views/Sysadmin/Permission.vue'),
			},
			{
				path: '/dist/log',
				name: 'Log',
				meta: {
					title: '监控日志',
					requireAuth: true
				},
				component: () => import ('../views/Sysadmin/Log.vue')
			},
			{
				path: '/dist/user',
				name: 'User',
				meta: {
					title: '用户管理',
					requireAuth: true
				},
				component: () => import ('../views/Sysadmin/User.vue')
			},
			// 学校工作人员
			{
				path: '/dist/onsitepay',
				name: 'OnsitePay',
				meta: {
					title: '现场缴费',
					requireAuth: true
				},
				component: () => import ('../views/Schoolwork/OnsitePay.vue')
			},
			{
				path: '/dist/bedroom',
				name: 'Bedroom',
				meta: {
					title: '入住办理',
					requireAuth: true
				},
				component: () => import ('../views/Schoolwork/Bedroom.vue')
			},
			{
				path: '/dist/checkin',
				name: 'Checkin',
				meta: {
					title: '院系报到',
					requireAuth: true
				},
				component: () => import ('../views/Schoolwork/Checkin.vue')
			},
			{
				path: '/dist/green',
				name: 'Green',
				meta: {
					title: '绿色通道办理',
					requireAuth: true
				},
				component: () => import ('../views/Schoolwork/Green.vue')
			},
			{
				path: '/dist/living',
				name: 'Living',
				meta: {
					title: '生活用品办理',
					requireAuth: true
				},
				component: () => import ('../views/Schoolwork/Living.vue')
			},
		]
	}
]

const router = createRouter({
	history: createWebHistory(process.env.BASE_URL),
	routes
})


router.beforeEach((to, from, next) => {
	if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
		let token = sessionStorage.getItem('token');
		if (token) { // 通过vuex state获取当前的token是否存在
			let menu = JSON.parse(store.state.menu); // 为了菜单的链接，直接跳转404
			if (menu && menu.length > 0) {
				// eslint-disable-next-line no-constant-condition
				if (true) {
					next();
					/* 路由发生变化修改页面meta */
					if (to.meta.content) {
						let head = document.getElementsByTagName('head');
						let meta = document.createElement('meta');
						meta.content = to.meta.content;
						head[0].appendChild(meta)
					}
					/* 路由发生变化修改页面title */
					if (to.meta.title) {
						document.title = to.meta.title;
					}
				}
			} else {
				next();
				if (to.meta.content) {
					let head = document.getElementsByTagName('head');
					let meta = document.createElement('meta');
					meta.content = to.meta.content;
					head[0].appendChild(meta);
				}
				if (to.meta.title) {
					document.title = to.meta.title;
				}
			}
		} else {
			next({
				path: '/dist/login',
			});
			ElMessage.warning('请先登录！');
		}
	} else {
		next();
		if (to.meta.content) {
			let head = document.getElementsByTagName('head');
			let meta = document.createElement('meta');
			meta.content = to.meta.content;
			head[0].appendChild(meta)
		}
		if (to.meta.title) {
			document.title = to.meta.title;
		}
	}
})


export default router
